package sg.techstudio.suntec.dao;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.velocity.app.VelocityEngine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

import sg.techstudio.suntec.email.GenericHTMLPreparator;
import sg.techstudio.suntec.object.CPLoginObj;

@Component("emailDao")
public class EmailDao {
	private static final Log log = LogFactory.getLog(EmailDao.class);
	@Autowired
	private VelocityEngine velocityEngine;
	
	@Autowired
	private JavaMailSender mailSender;
	
	@Autowired
	private EmailConfig vipAlertEmailConfig;
	
	public boolean sendVipAlertEmail(CPLoginObj cpLoginObj, String to){
		GenericHTMLPreparator emailPrep = vipAlertEmailPrep(cpLoginObj, to);
		Map<String, Object> emailInfoMap = new HashMap<String, Object>();
		emailInfoMap.put("cp", cpLoginObj);
		emailPrep.setModel(emailInfoMap);
		mailSender.send(emailPrep);
		return true;
	}
	
	public boolean sendEmail(CPLoginObj cpLoginObj, String to){
		log.debug("sendEmail() entry");
		return sendVipAlertEmail(cpLoginObj, to);
	}
	
	protected GenericHTMLPreparator vipAlertEmailPrep(CPLoginObj cpLoginObj, String to) {
		GenericHTMLPreparator emailPrep = new GenericHTMLPreparator();
		emailPrep.setVelocityEngine(velocityEngine);
		emailPrep.setFrom(vipAlertEmailConfig.getFrom());
		emailPrep.setTo(to);
		emailPrep.setCc(vipAlertEmailConfig.getCc());
		emailPrep.setBcc(vipAlertEmailConfig.getBcc());
		emailPrep.setSubject(vipAlertEmailConfig.getSubject());
		emailPrep.setSenderName(vipAlertEmailConfig.getSenderName());
		emailPrep.setTemplateLocation(vipAlertEmailConfig.getTemplateLocation());
		return emailPrep;
	}
	
	
	//for sending email manually
	
	protected static final String TEST_EMAIL = "dhany@techstudio.com.sg";
	private static EmailDao emailDao;
	private static String configLocation = "config/spring-context.xml";
	public static void main(String[] args) {
		System.out.println("start");
		try {
			if(args!=null && args.length>=1)
				configLocation = args[0];
			initProcess(configLocation);
//			RegisterObj r = registrationDao.getRegisterObj(1);
			
//			emailDao.sendParticipantEmail(getP(), getR());
//			emailDao.sendOtherCompanyEmail(getR());
//			emailDao.sendChequeEmail(getR());
			emailDao.sendEmail(getCPLogin(), TEST_EMAIL);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("EMailDao", e);
		}
		System.out.println("end");
	}
	
	protected static CPLoginObj getCPLogin() {
		CPLoginObj c = new CPLoginObj();
		c.setApName("24:de:c6:c2:9b:f7");
		c.setMac("40:30:04:df:3b:88");
		c.setIp("172.16.11.175");
		c.setSsid("silver@suntec");
		c.setVcName("Instant-C2:9B:F7");
		c.setUsername("username1243");
		
		return c;
	}
	
	@SuppressWarnings("resource")
	protected static void initProcess(String configLocation) {
		ApplicationContext ctx = new ClassPathXmlApplicationContext(configLocation);
		emailDao = ctx.getBean(EmailDao.class);
	}
}
